package online.flowerinsnow.minecraft.bukkit.testffa.manager;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.*;
import org.jetbrains.annotations.NotNull;

public class ScoreboardManager {
    private ScoreboardManager() {
    }

    public static void initPlayerScoreboard(@NotNull Player player) {
        Scoreboard sb = Bukkit.getScoreboardManager().getNewScoreboard();
        player.setScoreboard(sb);
        Objective objective = sb.registerNewObjective("side", "dummy");
        objective.setDisplayName("§b§lFFA");
        objective.setDisplaySlot(DisplaySlot.SIDEBAR);

        objective = sb.registerNewObjective("health", "dummy");
        objective.setDisplayName("§c❤");
        objective.setDisplaySlot(DisplaySlot.BELOW_NAME);

        Objective finalObjective = objective;
        Bukkit.getOnlinePlayers().forEach(p -> finalObjective.getScore(p.getName()).setScore((int) p.getHealth()));
    }

    public static void setLinePrefix(@NotNull Player player, int line, @NotNull String prefix) {
        Scoreboard sb = player.getScoreboard();
        Objective objective = sb.getObjective("side");
        Team team = sb.getTeam("sbline_" + line);

        if (team == null) {
            team = sb.registerNewTeam("sbline_" + line);
            team.addEntry("§" + ((char) line));
        }
        Score score = objective.getScore("§" + ((char) line));
        if (!score.isScoreSet()) { // 判断这个分数是否已经在积分项上了
            score.setScore(line);
        }
        team.setPrefix(prefix);
    }

    public static void setLineSuffix(@NotNull Player player, int line, @NotNull String suffix) {
        Scoreboard sb = player.getScoreboard();
        Objective objective = sb.getObjective("side");
        Team team = sb.getTeam("sbline_" + line);

        if (team == null) {
            team = sb.registerNewTeam("sbline_" + line);
            team.addEntry("§" + ((char) line));
        }
        Score score = objective.getScore("§" + ((char) line));
        if (!score.isScoreSet()) { // 判断这个分数是否已经在积分项上了
            score.setScore(line);
        }
        team.setSuffix(suffix);
    }

    public static void hideLine(@NotNull Player player, int line) {
        Scoreboard sb = player.getScoreboard();
        Objective objective = sb.getObjective("side");
        Score score = objective.getScore("§" + ((char) line));
        if (score.isScoreSet()) {
            sb.resetScores(score.getEntry());
        }
    }

    public static void initPlayerObjective(@NotNull Player player) {
        setLinePrefix(player, 1, "§eflowerinsnow.o");
        setLineSuffix(player, 1, "§enline");

        setLinePrefix(player, 2, "§a§m------------");
        setLineSuffix(player, 2, "§a§m--------");

        setLinePrefix(player, 3, "§7战斗对手: ");
        setLineSuffix(player, 3, "");
        hideLine(player, 3);

        setLinePrefix(player, 4, "§7战斗状态: ");
        setLineSuffix(player, 4, "§c0");
        hideLine(player, 4);

        setLinePrefix(player, 5, "");

        setLinePrefix(player, 6, "§7K/D: ");
        setLineSuffix(player, 6, "§7读取中...");

        setLinePrefix(player, 7, "§7死亡: ");
        setLineSuffix(player, 7, "§7读取中...");

        setLinePrefix(player, 8, "§7杀敌: ");
        setLineSuffix(player, 8, "§7读取中...");

        setLinePrefix(player, 9, "§a§m------------");
        setLineSuffix(player, 9, "§a§m--------");
    }
}
